<template>
  <div class="login-container">
    <div class="login-card">
      <div class="logo-box">
        <img alt="" class="logo" src="@/assets/img/login-logo.png" />
        <span class="logo-name">Neta Simulator</span>
      </div>
        <el-form ref="userForm" :model="userForm" :rules="rules" class="login-form">
          <el-form-item prop="username">
            <el-input
              v-model="userForm.username"
              placeholder="请输入账号"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="userForm.password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item prop="pwdAgain">
            <el-input
              v-model="userForm.pwdAgain"
              placeholder="请确认密码"
              prefix-icon="el-icon-lock"
              show-password
            ></el-input>
          </el-form-item>
          <el-button
            type="primary"
            class="loginBtn"
            v-loading.fullscreen.lock="fullscreenLoading"
            @click="handleRegister"
          >
            注册
          </el-button>
          <p class="tip">
            <router-link :to="{name:'Login'}"> 已有账号，前往登录</router-link>
          </p>
        </el-form>
      </div>
  </div>
</template>

<script>
import { register } from '@/api/login'
export default {
  data() {
    // 重复密码验证
    const checkPwdAgain = async(rule, value, callback) => {
      if (value === '') {
        return callback(new Error('重复密码不能为空！'))
      } else if (this.userForm.password !== this.userForm.pwdAgain) {
        return callback(new Error('两次输入密码不一致！'))
      } else {
        callback()
      }
    }
    return {
      fullscreenLoading: false,
      userForm: {
        username: '',
        password: '',
        pwdAgain: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在3到15个字符', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        pwdAgain: [
        { required: true, message: '请再次输入密码', trigger: 'blur' },
        { validator: checkPwdAgain, trigger: 'blur' }
      ]
      }
    }
  },
  methods: {
     handleRegister() {
      try {
        this.fullscreenLoading = true
          this.$refs.userForm.validate((valid) => {
            if (valid) { 
              // 删除重复密码的字段，不给后端传
              delete this.userForm.pwdAgain
              register(this.userForm).then(res => {
                this.fullscreenLoading = false
                if(res.code === 0){
                  this.$message({
                    message: '注册用户成功',
                    type: 'success',
                    center: true
                  })
                  this.$router.push('/login')
                }else{
                  this.$message({
                    message: res.data,
                    type: 'warning',
                    center: true
                  })
                }
                this.resetField()
              }).catch(() => {
                this.$message({
                    message: '注册用户失败',
                    type: 'error',
                    center: true
                  })
              })
          }else{
            this.fullscreenLoading = false
            this.$message({
              message: '信息校验失败',
              type: 'error',
              center: true
            })
          }
        }) 
      }
      catch (err) {
        this.fullscreenLoading = false
        throw err
      }
    },
    resetField(){
      this.userForm = {
        username: '',
        password: '',
        pwdAgain: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/vars.scss';
.login-container {
  width: 100vw;
  height: 100vh;
  box-shadow: 5px 4px 100px 74px #ffffff inset;
  background: url("~@/assets/img/login-bg.png");
  background-attachment: fixed;
  background-size: cover;
  background-repeat: no-repeat;
  .login-card {
    position: absolute;
    width: 386px;
    height: 480px;
    right: 8%;
    padding: 10px;
    top: calc(50% - 300px);
    background: #fff;
    border-radius: 10px;
    border: 1px solid rgba(0, 0, 0, 0.12);
    .logo-box {
      margin: 40px auto 40px;
      margin-left: 36px;
    }

    .logo {
      height: 48px;
      margin-right: 5px;
      vertical-align: middle;
    }

    .logo-name {
      position: relative;
      top: 8px;
      color: rgb(38 144 237);
      font-weight: 600;
      font-size: 26px;
    }
    .login-form{
      text-align: center;
      .el-form-item{
        margin-bottom: 32px;
      }
      ::v-deep .el-input{
        .el-input__inner{
          width: 318px;
          height: 42px;
        }
        .el-input__prefix{
          left: 24px;
        }
        .el-input__suffix{
          right: 24px;
        } 
      }
      ::v-deep .el-form-item__error{
          left: 24px !important;
        }
      .loginBtn{
        width: 318px;
      }
    }
    
  }
  .tip{
    margin-top: 10px;
    float: right;
    margin-right: 24px;
    font-size: 13px;
    a:hover{
      color: $primary-color !important;
    }
  }
}
</style>
